package com.leinao.file.controller;

import com.alibaba.fastjson.JSON;
import com.leinao.commons.Response;
import com.leinao.constant.Constant;
import com.leinao.file.dto.FilePageDto;
import com.leinao.file.service.FileService;
import com.leinao.login.dto.LoginResultDto;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Arrays;

/**
 *
 * @Description 文件下载控制类
 * @author lizi
 * @date 2018/10/16 上午11:19
 * @Version 1.0
 */
@RestController
@CrossOrigin
public class FileController {
    private static Logger logger = LoggerFactory.getLogger(FileController.class);

    @Autowired
    private FileService fileService;

    @Autowired
    private HttpServletRequest request;

    private boolean timeoutSwitch = true;

    @ApiOperation(value="获取文件列表", notes="返回文件列表")
    @GetMapping(value = "/file/getFileList")
    public Response<FilePageDto> getFileList(@ApiParam(value = "业务类型: 1-文件中心，2-项目根目录，3-个人数据集和公开数据集", required = true) @RequestParam Integer type,
                                             @ApiParam(value = "目录,只传userhome后的目录即可，以/开头", required = true) @RequestParam String dir,
                                             @ApiParam(value = "当前页") @RequestParam(required = false) Integer pageNumber,
                                             @ApiParam(value = "每业条数") @RequestParam(required = false) Integer pageSize) {
    	logger.info("getFileList param type : {}, dir: {}, pageNumber: {}, pageSize: {}", type, dir, pageNumber, pageSize);

        // 默认分页条件设置
        if(pageNumber == null || pageNumber < 1) {
            pageNumber = Constant.DEFAULT_PAGE_NUMBER;
        }
        if(pageSize == null || pageSize < 1) {
            pageSize = Constant.DEFAULT_PAGE_SIZE;
        }

        // 获取登录用户信息
        String obj = (String) request.getSession().getAttribute(Constant.USER_SESSION_KEY);
        LoginResultDto userInfo = JSON.parseObject(obj,LoginResultDto.class );

        return fileService.getFileList(userInfo.getUserId(), type, dir, pageNumber, pageSize);
    }

    @ApiOperation(value="创建文件夹", notes="返回文件夹创建结果")
    @PostMapping(value = "/file/addDir")
    public Response<Void> addDir(@ApiParam(value = "业务类型: 1-文件中心，2-项目根目录，3-个人数据集和公开数据集", required = true) @RequestParam Integer type,
                                 @ApiParam(value = "目录", required = true) @RequestParam String dir) {
    	logger.info("addDir param type : {}, dir: {}", type, dir);

        // 获取登录用户信息
        String obj = (String) request.getSession().getAttribute(Constant.USER_SESSION_KEY);
        LoginResultDto userInfo = JSON.parseObject(obj,LoginResultDto.class );

        return fileService.addDir(userInfo.getUserId(), type, dir);
    }

    @ApiOperation(value="删除文件或目录", notes="返回删除文件或目录的结果")
    @PostMapping(value = "/file/deleteFile")
    public Response<Void> deleteFile(@ApiParam(value = "业务类型: 1-文件中心，2-项目根目录，3-个人数据集和公开数据集", required = true) @RequestParam Integer type,
                                     @ApiParam(value = "目录", required = true) @RequestParam String[] dirs) {
    	logger.info("deleteFile param type : {}, dirs: {}", type, Arrays.toString(dirs));

        // 获取登录用户信息
        String obj = (String) request.getSession().getAttribute(Constant.USER_SESSION_KEY);
        LoginResultDto userInfo = JSON.parseObject(obj,LoginResultDto.class );

        return fileService.deleteFile(userInfo.getUserId(), type, dirs);
    }

    @ApiOperation(value="文件或文件夹重命名", notes="返回文件或文件夹重命名结果")
    @PostMapping(value = "/file/renameFile")
    public Response<Void> renameFile(@ApiParam(value = "业务类型: 1-文件中心，2-项目根目录，3-个人数据集和公开数据集", required = true) @RequestParam Integer type,
                                     @ApiParam(value = "旧目录名称", required = true) @RequestParam String dir,
                                     @ApiParam(value = "新目录名称", required = true) @RequestParam String newDir) {
		logger.info("renameFile param type : {}, dir: {}, newDir: {}", type, dir, newDir);

        // 获取登录用户信息
        String obj = (String) request.getSession().getAttribute(Constant.USER_SESSION_KEY);
        LoginResultDto userInfo = JSON.parseObject(obj,LoginResultDto.class );

		return fileService.renameFile(userInfo.getUserId(), type, dir, newDir);
	}

    @ApiOperation(value="文件或文件夹复制", notes="返回文件或文件夹复制结果")
    @PostMapping(value = "/file/copyFile")
    public Response<Void> copyFile(@ApiParam(value = "业务类型: 1-文件中心，2-项目根目录，3-个人数据集和公开数据集", required = true) @RequestParam Integer type,
                                   @ApiParam(value = "旧目录名称", required = true) @RequestParam String[] dirs) {
		logger.info("copyFile param type : {}, dirs: {}", type, Arrays.toString(dirs));

        // 获取登录用户信息
        String obj = (String) request.getSession().getAttribute(Constant.USER_SESSION_KEY);
        LoginResultDto userInfo = JSON.parseObject(obj,LoginResultDto.class );

		return fileService.copyFile(userInfo.getUserId(), type, dirs);
	}

    @ApiOperation(value="获取公开模型文件列表", notes="返回公开模型文件列表")
    @GetMapping(value = "/file/getFileListForModel")
    public Response<FilePageDto> getFileList(@ApiParam(value = "业务类型: 1-文件中心，2-项目根目录，3-个人数据集和公开数据集", required = true) @RequestParam Integer type,
                                             @ApiParam(value = "目录,只传userhome后的目录即可，以/开头", required = true) @RequestParam String dir,
                                             @ApiParam(value = "模型创建者ID", required = true) @RequestParam String userId,
                                             @ApiParam(value = "当前页") @RequestParam(required = false) Integer pageNumber,
                                             @ApiParam(value = "每业条数") @RequestParam(required = false) Integer pageSize) {
        logger.info("getFileList param type : {}, dir: {}, pageNumber: {}, pageSize: {}", type, dir, pageNumber, pageSize);

        // 默认分页条件设置
        if(pageNumber == null || pageNumber < 1) {
            pageNumber = Constant.DEFAULT_PAGE_NUMBER;
        }
        if(pageSize == null || pageSize < 1) {
            pageSize = Constant.DEFAULT_PAGE_SIZE;
        }

        // 获取登录用户信息
        String obj = (String) request.getSession().getAttribute(Constant.USER_SESSION_KEY);
        LoginResultDto userInfo = JSON.parseObject(obj,LoginResultDto.class );

        return fileService.getFileList(userId, type, dir, pageNumber, pageSize);
    }
}
